﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Labthink.PCSoft.UniversalHost4.Utility;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class UcPassword 
    {
        public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(UcPassword), new PropertyMetadata("Label"));

        public string Label
        {
            get => (string)GetValue(LabelProperty);
            set => SetValue(LabelProperty, value);
        }

        public static readonly DependencyProperty PasswordProperty = DependencyProperty.Register("Password", typeof(string), typeof(UcPassword), new PropertyMetadata("",OnPasswordChangedCallback));

        public string Password
        {
            get => (string)GetValue(PasswordProperty);
            set => SetValue(PasswordProperty, value);
        }

        private static void OnPasswordChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is UcPassword p)
            {
                p.ChangePassword(e.NewValue+"");
            }
        }

        public static readonly DependencyProperty MaxLengthProperty = DependencyProperty.Register("MaxLength", typeof(double), typeof(UcPassword), new PropertyMetadata(65535.0));

        public double MaxLength
        {
            get => (double)GetValue(MaxLengthProperty);
            set => SetValue(MaxLengthProperty, value);
        }

        public PasswordBox Pwd => Txt;

        private System.Windows.Media.Brush _tmpBrush;

        public void ChangePassword(string pw)
        {
            Txt.Password = pw;
            Txt1.Password = pw;
        }

        public UcPassword()
        {
            InitializeComponent();
            _tmpBrush = Txt.BorderBrush;
        }

        private void Txt_OnGotFocus(object sender, RoutedEventArgs e)
        {
            Txt.BorderBrush = _tmpBrush;
            _openInput();
        }

        private void _openInput()
        {
            //string file= Keyboard;
            //if (!File.Exists(file)) return;
            ////POINT pt = new POINT();
            ////GetCursorPos(out pt);
            //var pt = PointToScreen(Mouse.GetPosition(Txt));
            //Process.Start(file, $"{pt.X} {pt.Y}");
        }

        private bool _isOpen()
        {
            //string name = "KeyboardWindow";
            //var ptr = FindWindow(null, name);
            return true;
        }

        private void Txt_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            pop1.IsOpen = false;
            pop1.IsOpen = true;
            pop1.StaysOpen = false;
            pop1.StaysOpen = true;
        }

        private void Txt_PreviewTouchDown(object sender, TouchEventArgs e)
        {
            pop1.IsOpen = false;
            pop1.IsOpen = true;
            pop1.StaysOpen = false;
            pop1.StaysOpen = true;
        }

        private void Txt_OnLostFocus(object sender, RoutedEventArgs e)
        {
            if (!Txt1.IsFocused) pop1.StaysOpen = false;
            if (Txt.Password.Length > MaxLength)
            {
                _tmpBrush = Txt.BorderBrush;
                Txt.BorderBrush = System.Windows.Media.Brushes.Red;
                Txt.Password = Txt.Password.Substring(0, MaxLength.ToInt());
            }
        }

        public void Txt1_OnLostFocus(object sender, RoutedEventArgs e)
        {
            pop1.StaysOpen = false;
            pop1.IsOpen = false;
        }

        private void Txt_OnTextChanged(object sender, RoutedEventArgs e)
        {
            if (sender is PasswordBox txt)
            {
                if (txt.Password != Txt.Password) Txt.Password = txt.Password;
                if (txt.Password != Txt1.Password) Txt1.Password = txt.Password;
            }
        }
    }
}
